package com.example.aics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aics.dto.AppointMentDTO;
import com.example.aics.entity.Appointment;
import com.example.aics.mapper.AppointmentMapper;
import com.example.aics.service.AppointmentService;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Override
    public List<Appointment> findByIdCard(String idCard) {
        return baseMapper.findByIdCard(idCard);
    }

    @Override
    public Appointment findOne(AppointMentDTO queryDTO) {
        LambdaQueryWrapper<Appointment> queryWrapper = new LambdaQueryWrapper<>();
    
        if(StringUtils.isNotEmpty(queryDTO.getIdCard())){
            queryWrapper.eq(Appointment::getIdCard, queryDTO.getIdCard());
        }
        if(StringUtils.isNotEmpty(queryDTO.getUserName())){
            queryWrapper.eq(Appointment::getUserName, queryDTO.getUserName());}
        if(StringUtils.isNotEmpty(queryDTO.getDepartment())){
            queryWrapper.eq(Appointment::getDepartment, queryDTO.getDepartment());
        }
        if(StringUtils.isNotEmpty(queryDTO.getDate())){
            queryWrapper.eq(Appointment::getDate, queryDTO.getDate());
        }
        if(StringUtils.isNotEmpty(queryDTO.getTime())){
            queryWrapper.eq(Appointment::getTime, queryDTO.getTime());
        }
        queryWrapper.orderByDesc(Appointment::getId);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }
}
